package fmod.org.smali.myapplication.views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;
import android.widget.ViewFlipper;


public class GestureTestView extends ViewFlipper {
    private static String TAG = "GestureTestView";
    private GestureDetector mGestureDetector;
    private Scroller mScroller;
    private FlipperListener mFlipperListener;

    public static final int FLIP_TO_RIGHT = 1;
    public static final int FLIP_TO_LEFT = -1;

    final int mScrollerDurationTime = 400;
    int distanceX;
    private boolean isStarFlipTag;
    private boolean isFlipCenterTag;

    interface FlipperListener {

        void startFlip(int flipDuration);

        void flipToCenter(int flipDuration);

        void flipFinish(int flipDuration);
    }
    public GestureTestView(Context context) {
        super(context);
        init();
    }
    public GestureTestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    private void init() {
        mGestureDetector = new GestureDetector(getContext(), new MyGesture());
        mScroller = new Scroller(getContext(), new DecelerateInterpolator());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                int left = getCurrentView().getLeft();
                if (left > getMeasuredWidth() / 2) {
                    mScroller.startScroll(left, 0, getMeasuredWidth() - getCurrentView().getLeft(), 0, mScrollerDurationTime);
                } else if (left > 0) {
                    mScroller.startScroll(left, 0, -left, 0, mScrollerDurationTime);
                } else if (left < (-getMeasuredWidth()) / 2) {
                    mScroller.startScroll(left, 0, -getMeasuredWidth() - left, 0, mScrollerDurationTime);
                } else if (left < 0) {
                    mScroller.startScroll(left, 0, -left, 0, mScrollerDurationTime);
                }
                requestLayout();
                break;
        }
        return mGestureDetector.onTouchEvent(event);
    }
    class MyGesture extends GestureDetector.SimpleOnGestureListener {
        String TAG = "GestureTAG";
        @Override
        public boolean onDown(MotionEvent e) {
            Log.i(TAG, "onDown");
            return true;
        }
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Log.i(TAG, "onScroll");
            GestureTestView.this.distanceX = -(int) (e1.getX() - e2.getX());
            requestLayout();
            return true;
        }
    }

    int flipDuration = 0;
    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            distanceX = mScroller.getCurrX();
            requestLayout();
        } else {

        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        getCurrentView().offsetLeftAndRight(distanceX);
        if (distanceX > 0) {
            setNextViewToCurrentViewLeft();
        } else {
            setNextViewToCurrentViewRight();
        }

        int currentViewLeft = getCurrentView().getLeft();
        if (currentViewLeft > 0) {
            flipDuration = FLIP_TO_RIGHT;
        } else {
            flipDuration = FLIP_TO_LEFT;
        }

        if (currentViewLeft == getMeasuredWidth()) {
            //滑动成功
            switchCurrentView();
            if (mFlipperListener != null)
                mFlipperListener.flipFinish(flipDuration);
        } else if (Math.abs(currentViewLeft) >= getMeasuredWidth() / 2 &&!isFlipCenterTag) {
            isFlipCenterTag = true;
            //滑动到中间
            if (mFlipperListener != null)
                mFlipperListener.flipToCenter(flipDuration);
        } else if (Math.abs(distanceX) > 0&&!isStarFlipTag) {
            isStarFlipTag = true;
            //滑动开始
            if (mFlipperListener != null)
                mFlipperListener.startFlip(flipDuration);
        }
        Log.i(TAG, "onLayout---currentViewLeft" + getCurrentView().getLeft());
    }

    private void switchCurrentView() {
        boolean b = getChildAt(0) == getCurrentView();
        if (b) {
            setDisplayedChild(0);
        }
    }

    private void setNextViewToCurrentViewLeft() {
        View nextView = getNextView();
        int currentViewLeft = getCurrentView().getLeft();
        int nextViewLeft = currentViewLeft - nextView.getMeasuredWidth();
        nextView.layout(nextViewLeft, nextView.getTop(), currentViewLeft, nextView.getBottom());
    }

    private void setNextViewToCurrentViewRight() {
        View nextView = getNextView();
        int currentViewRight = getCurrentView().getRight();
        int nextViewRight = getCurrentView().getMeasuredWidth() + currentViewRight;
        nextView.layout(currentViewRight, nextView.getTop(), nextViewRight, nextView.getBottom());
    }

    public View getNextView() {
        byte var1 = 1;
        if (this.getChildAt(1) == this.getCurrentView()) {
            var1 = 0;
        }
        View nextView = this.getChildAt(var1);
        nextView.setVisibility(View.VISIBLE);
        return nextView;
    }

    public void setFlipperListener(FlipperListener flipperListener) {
        mFlipperListener = flipperListener;
    }
}
